// Quak Bak 0.3 | http://quakbak.com
// 
// Copyright (c) 2009 Jeremy Hilton 
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

$(document).ready(function() {
//    $.getJSON(url_to_shorten_lib + "?url="+document.location.href, function(data) {
$.getJSON(url_to_shorten_lib + "?url=http://yahoo.com", function(data) {
 
        var urls = [];
        $.each(data.urls, function(key, value) {
            urls.push(value);
        });

        $.getJSON('http://search.twitter.com/search.json?rpp=100&q=&ors=' + urls.join("+") + '&callback=?',
            function(twitter_data) {
                var tweet_backs = [],
                tweet_back_list = [],
		tb_count_json = [],
                t_count = 0;
 
                $.each(twitter_data.results, function(key, item) {
			results = document.getElementById("tweetbacks");
			results.innerHTML += tmpl("tweet_back_tmpl", item);
			t_count++;
                });

		// create the TB count JSON object for use in the TB count template
		tb_count_json = {"tb_count":t_count};

		results = document.getElementById("tweetbackcount");
		results.innerHTML += tmpl("tweet_back_count_tmpl", tb_count_json);
 
            }
        );
    });
});


// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
	var cache = {};

	this.tmpl = function tmpl(str, data){
    	// Figure out if we're getting a template, or if we need to
    	// load the template - and be sure to cache the result.
    	var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :

      	// Generate a reusable function that will serve as a template
      	// generator (and which will be cached).
      	new Function("obj",
        	"var p=[],print=function(){p.push.apply(p,arguments);};" +

        	// Introduce the data as local variables using with(){}
        	"with(obj){p.push('" +

        	// Convert the template into pure JavaScript
        	str
          	.replace(/[\r\t\n]/g, " ")
          	.split("<%").join("\t")
          	.replace(/((^|%>)[^\t]*)'/g, "$1\r")
          	.replace(/\t=(.*?)%>/g, "',$1,'")
          	.split("\t").join("');")
          	.split("%>").join("p.push('")
          	.split("\r").join("\\'")
      		+ "');}return p.join('');"
		);
  
    	// Provide some basic currying to the user
    	return data ? fn( data ) : fn;
  	};
})();

